package thread.productcomsumer;

import java.util.LinkedList;

public class Source {
    private int max;
    private LinkedList data;

    public Source(int max) {
        this.max = max;
        this.data = new LinkedList();
    }

    // 消费
    public synchronized Object  poll() throws InterruptedException {
        while(data.size() == 0){
            wait();
        }
        Object result = data.poll();
        notify();
        return result;
    }

    // 生产
    public synchronized void offer(Object element) throws InterruptedException {
        while(data.size() > max){
            wait();
        }
        data.offer(element);
        notify();
    }
}
